package com.xixixi.controller;

import com.xixixi.po.Items;
import com.xixixi.service.ItemsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/items")
public class ItemsController {

    @Autowired
    private ItemsService itemsService;

    private static Logger logger= LoggerFactory.getLogger(ItemsController.class);

    @RequestMapping("/queryItems")
    public String queryItems(Model model) throws Exception{
        List<Items> itemsList=itemsService.findItemsByName("");
        logger.info("log test:{}",itemsList);
        model.addAttribute("itemsList",itemsList);
        return "itemsList";
    }

    @RequestMapping(value = "/editItem")
    public String editItem(int id,Model model) throws Exception{
        Items item=itemsService.findItemById(id);
        model.addAttribute("item",item);
        return "editItem";
    }

    @RequestMapping("/doEdit")
    public String doEdit(Items item, MultipartFile pictureFile,HttpServletRequest request) throws Exception {

        //上传文件
        if(!pictureFile.isEmpty()) {
            //上传文件路径 (在target下)
            String path = request.getSession().getServletContext().getRealPath("/images/");
            //上传文件名
            String filename = pictureFile.getOriginalFilename();
            //一般来说为防止重复为文件重命名
            //String newFilename=UUID.randomUUID()+filename.substring(filename.lastIndexOf("."));
            //创建文件上传路径
            File filepath = new File(path,filename);
            //判断路径是否存在，如果不存在就创建一个
            if (!filepath.getParentFile().exists()) {
                filepath.getParentFile().mkdirs();
            }
            //将上传文件保存到磁盘
            pictureFile.transferTo(new File(path + File.separator + filename));


            //将文件名存入数据库以便下次通过名称读取相应的文件
            //对于图片直接在前端页面使用img 利用图片名就可以显示图片，如上传了123.jpg到/images/下
            //那么下次可以直接在<img>中写url /images/${user.pic}来显示图片


            return "success";
        } else {
            return "error";
        }
    }

    @RequestMapping("/jsonTest")
    public String jsonTest(){
        return "jsonTest";
    }

    // 商品修改提交json信息，响应json信息
    @RequestMapping("/editItemSubmit_RequestJson")
    public @ResponseBody Items editItemSubmit_RequestJson(@RequestBody Items items) throws Exception {
        System.out.println(items);
        logger.info("xixixi test");
        //itemService.saveItem(items);
        return items;

    }

    @RequestMapping("/editItemSubmit_ResponseJson")
    public @ResponseBody Items editItemSubmit_ResponseJson(Items items) throws Exception {
        System.out.println(items);
        //itemService.saveItem(items);
        return items;

    }

}
